from algorithm.cluster.k_means_cluster import KMeansCluster


class ClusterGateway:
    def __init__(self):
        self.dataTable = [
            {"id": 0, "name": "Cluster1", "size": 11, "detail": "Data for test"},
            {"id": 1, "name": "Cluster2", "size": 30, "detail": "More data for test"},
            {"id": 2, "name": "Cluster3", "size": 100, "detail": "Telecomunication"}]

        self.dataType = [
            [{"attr": "num0", "type": "Int", "enabled": True},
             {"attr": "num1", "type": "Int", "enabled": True},
             {"attr": "num2", "type": "Int", "enabled": True},
             {"attr": "num3", "type": "Int", "enabled": True}],
            [{"attr": "num0", "type": "Int", "enabled": True},
             {"attr": "num1", "type": "Int", "enabled": True},
             {"attr": "num2", "type": "Int", "enabled": True},
             {"attr": "num3", "type": "Int", "enabled": True},
             {"attr": "num4", "type": "Int", "enabled": True}],
            [{"attr": "Tel", "type": "Text", "enabled": False},
             {"attr": "Sex", "type": "Binary", "enabled": True},
             {"attr": "Age", "type": "Int", "enabled": True},
             {"attr": "Fee", "type": "Float", "enabled": True},
             {"attr": "Dur", "type": "Int", "enabled": True},
             {"attr": "Cou", "type": "Int", "enabled": True}]]

        self.data = [
            [(5, 3, 4, 2),
             (2, 4, 1, 3),
             (1, 2, 3, 1),
             (2, 4, 1, 3),
             (6, 4, 5, 3),
             (4, 2, 3, 1),
             (2, 4, 5, 3),
             (3, 1, 2, 4),
             (1, 4, 1, 3),
             (4, 2, 3, 1),
             (3, 1, 2, 4)],
            [(5, 3, 4, 2, 1),
             (2, 4, 1, 3, 2),
             (1, 2, 3, 1, 5),
             (2, 3, 1, 3, 1),
             (6, 1, 5, 3, 4),
             (4, 2, 3, 1, 2),
             (2, 3, 1, 3, 1),
             (6, 1, 5, 3, 4),
             (4, 2, 3, 1, 2),
             (2, 4, 2, 5, 3),
             (6, 2, 2, 3, 1),
             (4, 5, 3, 1, 2),
             (1, 4, 2, 5, 3),
             (3, 1, 4, 2, 4),
             (1, 4, 2, 4, 3),
             (4, 2, 6, 3, 1),
             (3, 1, 5, 2, 4),
             (1, 5, 2, 1, 3),
             (4, 2, 6, 3, 1),
             (2, 4, 2, 5, 3),
             (6, 2, 2, 3, 1),
             (4, 5, 3, 1, 2),
             (1, 4, 2, 5, 3),
             (3, 1, 4, 2, 4),
             (1, 4, 2, 4, 3),
             (4, 2, 6, 3, 1),
             (3, 1, 5, 2, 4),
             (1, 5, 2, 1, 3),
             (4, 2, 6, 3, 1),
             (5, 3, 1, 2, 4)]]

        f = open("test_data.data", "r")
        line = f.readline()
        d = []
        for line in f:
            s = line.split(",")
            d.append((str(s[0]), int(s[1]), int(s[2]),
                      float(s[3]), int(s[4]), int(s[5])))
        self.data.append(d)

    def getDataTable(self):
        return {"dataTable": self.dataTable}

    def getDataType(self, dataId):
        return {"dataType": self.dataType[dataId]}

    def kMeansCluster(self, dataId, enableList, n):
        k = KMeansCluster(self.data[dataId],
                          [x["type"] for x in self.dataType[dataId]], enableList)
        result = k.getclusters(n)
        return {"result": result}

